/**
  ******************************************************************************
  * @file    kea128_adc.h
  * @author  huaxicunzhang
  * @version V1.0
  * @date    18-8-2015
  * @brief   This file contains all the functions prototypes for the GPIO 
  *          firmware library.
  ******************************************************************************
**/ 

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __KEA128_ADC_H
#define __KEA128_ADC_H
#ifdef __cplusplus
 extern "C" {
#endif
#include "SKEAZ1284.h" 

#define CLOCK_SOURCE_BUS_CLOCK							0x00	/*!< ADC clock source is bus clock*/
#define CLOCK_SOURCE_BUS_CLOCK_DIVIDE_2			        0x01	/*!< ADC clock source is bus clock devided by 2*/
#define CLOCK_SOURCE_ALTCLK								0x02	/*!< ADC clock source is alternative clcok*/ //交替时钟
#define CLOCK_SOURCE_ADACK								0x03	/*!< ADC clock source is asynchronous clock*/ //异步时钟

#define ADC_ADIV_DIVIDE_1								0x00		/*!< ADC clock divide by 1*/
#define ADC_ADIV_DIVIDE_2								0x01		/*!< ADC clock divide by 2*/
#define ADC_ADIV_DIVIDE_4								0x02		/*!< ADC clock divide by 4*/
#define ADC_ADIV_DIVIDE_8								0x03		/*!< ADC clock divide by 8*/

	 
typedef enum
{
  ADC_CHANNEL_AD0                 =0x0, /*!< ADC input channel 0 */
  ADC_CHANNEL_AD1                 =0x1,/*!< ADC input channel 1 */
  ADC_CHANNEL_AD2                 =0x2, /*!< ADC input channel 2 */
  ADC_CHANNEL_AD3                 =0x3, /*!< ADC input channel 3 */
  ADC_CHANNEL_AD4                 =0x4, /*!< ADC input channel 4 */
  ADC_CHANNEL_AD5                 =0x5, /*!< ADC input channel 5 */
  ADC_CHANNEL_AD6                 =0x6, /*!< ADC input channel 6 */
  ADC_CHANNEL_AD7                 =0x7, /*!< ADC input channel 7 */
  ADC_CHANNEL_AD8                 =0x8, /*!< ADC input channel 8 */
  ADC_CHANNEL_AD9                 =0x9, /*!< ADC input channel 9 */
  ADC_CHANNEL_AD10                =0xa, /*!< ADC input channel 10 */
  ADC_CHANNEL_AD11                =0xb, /*!< ADC input channel 11 */
  ADC_CHANNEL_AD12                =0xc, /*!< ADC input channel 12 */
  ADC_CHANNEL_AD13                =0xd, /*!< ADC input channel 13 */
  ADC_CHANNEL_AD14                =0xe, /*!< ADC input channel 14 */
  ADC_CHANNEL_AD15                =0xf, /*!< ADC input channel 15 */
  ADC_CHANNEL_AD18_VSS            =0x12, /*!< ADC input channel VSS */ 
  ADC_CHANNEL_AD22_TEMPSENSOR     =0x16, /*!< ADC input channel internal temperature sensor */
  ADC_CHANNEL_AD23_BANDGAP        =0x17, /*!< ADC input channel bandgap */ 
  ADC_CHANNEL_AD29_VREFH          =0x1D, /*!< ADC input channel Vrefh */ 
  ADC_CHANNEL_AD30_VREFL          =0x1E, /*!< ADC input channel Vrefl */ 
  ADC_CHANNEL_DISABLE             =0x1F, /*!< ADC disable */
} ADCHn;	
#define IS_ADC_CHANNEL(CHANNEL)  (CHANNEL) < 0x20

typedef enum ADC_nbit
{
    ADC_8BIT   = 0x00,
    ADC_10BIT  = 0x01,
    ADC_12BIT  = 0x02,
} ADC_nbit;

#define IS_ADC_nbit(nbit)  (((nbit)== ADC_8BIT) || \
                            ((nbit)== ADC_10BIT) || \
                            ((nbit)== ADC_12BIT) )

#define ADC_FIFO_DISABLE                                0 /*!< FIFO Level 0 */
#define ADC_FIFO_LEVEL2                                 1 /*!< FIFO Level 1 */
#define ADC_FIFO_LEVEL3                                 2 /*!< FIFO Level 2 */
#define ADC_FIFO_LEVEL4                                 3 /*!< FIFO Level 3 */
#define ADC_FIFO_LEVEL5                                 4 /*!< FIFO Level 4 */
#define ADC_FIFO_LEVEL6                                 5 /*!< FIFO Level 5 */
#define ADC_FIFO_LEVEL7                                 6 /*!< FIFO Level 6 */
#define ADC_FIFO_LEVEL8                                 7	/*!< FIFO Level 7 */

#define IS_ADC_FIFO(FIFO)                               FIFO<8


#define HardwareTrigger     1
#define SoftwareTrigger     0

typedef struct
{
	
	uint8_t  ADC_CLK ;     /*ADC时钟     CLOCK_SOURCE_BUS_CLOCK			  CLOCK_SOURCE_BUS_CLOCK_DIVIDE_2
																			 CLOCK_SOURCE_ALTCLK					CLOCK_SOURCE_ADACK	  */		
  uint8_t ADC_ADIV ;     /*ADC分频     ADC_ADIV_DIVIDE_1            ADC_ADIV_DIVIDE_2
																			 ADC_ADIV_DIVIDE_4						ADC_ADIV_DIVIDE_8*/
  ADC_nbit ADC_Mode;                      /*!< Configures the ADC to operate in independent or
                                               dual mode. 
                                               This parameter can be a value of @ref ADC_mode */
  uint8_t ADC_FIFO;
	
	ADCHn ADC_CHx;
	
	uint8_t ADC_Trigger;
 
}ADC_InitTypeDef;


u16 adc_once(ADCHn adcn_ch, ADC_nbit bit); 

void ADC_Init(ADC_InitTypeDef *ADC_InitStruct);

void ADC_ITConfig(ADC_Type* pADC,FunctionalState NewState);

uint16_t ADC_GetConversionValue(ADCHn ch);
#endif	


